var app = getApp(), fetchApi = require("../../../../utils/httpPost.js")
import Toast from '../../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading:false,
    band_user_id:0,
    estate_id:0,
    room_no:'',
    phone_num:'',
    commentContent:'',
    fileList1: [],
    roomList:[],
    submitState:false,
    checkTypeRadio:'0',
    estate_names:[],
    isShowPopupRoom:false,
    roomId:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onHttpInit();
  },
  onShow(){
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/user/getAccountInfo', {}, function (res) {
      console.log("账号绑定 数据", res)
      if(res.data.code == 0){
        var user = res.data.data.user;
        that.data.estate_id = user.estateId; 
        if(res.data.data.rooms != undefined && res.data.data.rooms.length > 0){
          that.data.roomId = res.data.data.rooms != undefined ? res.data.data.rooms[0].id : 0;
        }
        that.data.estate_names = res.data.data.estate_names;
        that.setData({
          user: user,
          roomList: res.data.data.rooms,
          estate_names: res.data.data.estate_names,
          phone_num: user.phoneNum!=undefined &&  user.phoneNum!='' ? user.phoneNum : '',
          estate_name: res.data.data.estate_names!=undefined ? res.data.data.estate_names[0] : "",          
        })
      }
      that.setData({
        netWork: true,
      })
    }, function () {
        that.setData({ netWork: false, hasData: false })
    })
  },
  onChangeRoom(e){
    console.log('onChangeRoom',e)
    var index = e.detail.index;
    // console.log('onChangeRoom',this.data.roomList[index])
    let room = this.data.roomList[index];
    this.data.roomId = room.id;

    this.data.estate_name = this.data.estate_names[index]; 
    this.setData({
      estate_name: this.data.estate_names[index],
      isShowPopupRoom: !this.data.isShowPopupRoom,
    });

  },
  trim: function (t) {
    return t.replace(/(^\s*)|(\s*$)/g, "");
  },
  onCheckTypeClick(val){
    console.log('onCheckTypeClick',val)
    this.data.checkTypeRadio = val.currentTarget.dataset.name;
    this.setData({
      checkTypeRadio: this.data.checkTypeRadio
    })
  },
  submitOp:function(){
    var that = this, commentContent = this.trim(that.data.commentContent);
    console.log('commentContent',commentContent)
    console.log('commentContent.length', that.data.commentContent.length)
    if (this.data.phone_num.length <= 0){
      Toast.fail('手机号不能为空');
      return;
    }
    if (this.data.commentContent.length <= 0){
      Toast.fail('情况说明不能为空');
      return;
    }

    if(that.data.submitState){
      Toast.fail('正在提交...');
      return;
    }
    that.data.submitState = true;
    that.setData({submitState : true })

    fetchApi.httpPost('/repair/record/add', {
      phone_num:that.data.phone_num,
      content: commentContent,
      room_id: that.data.roomId,
      check_type: that.data.checkTypeRadio,
      record_type: 0,
    }, function (res) {
      console.log("报修上报返回数据", res)
      if (res.data.code == 0) {
        that.data.submitState = false;
        that.setData({submitState : false })
        var record_id = parseInt(res.data.data)
        Toast.success(res.data.msg);
        if (record_id > 0 && that.data.fileList1.length > 0) {
          for (var index in that.data.fileList1) {
            console.log(' that.data.fileList1', that.data.fileList1)
            wx.uploadFile({
              url: fetchApi.getPlatformUrl() + '/repair/record/uploadImg',
              filePath: that.data.fileList1[index].url,
              name: 'file',
              formData: { record_id: record_id, info_id:0, img_type:0,},
              success: function (res) {
                console.log('uploadFile success res', res)
              }, fail: function (res) {
                console.log('uploadFile fail res', res)
              }
            })
          }
        }
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 1000)
      }else{
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.fail( res.data.msg);
        return;
      }
    },function(){})
  },
  bindInput: function (t) {
    console.log("情况说明输入", t)
    this.data.commentContent = t.detail || "";
  },
  phoneNumInput: function (t) {
    console.log("手机号输入", t)
    this.data.phone_num = t.detail || "";
  },
  deleteImg:function(res){
      console.log('删除图片', res)
      var index = res.currentTarget.dataset.index;
      this.data.fileList1 = [];
      this.setData({
        fileList1: this.data.fileList1
      });
  },
  afterRead(event) {
    console.log('afterRead',event)
    const { file, name } = event.detail;
    console.log('afterRead name',name)
    console.log('afterRead file',file)
    const fileList = this.data[`fileList${name}`];
    
    this.setData({ [`fileList${name}`]: fileList.concat(file) });
    
    console.log('afterRead fileList',fileList)
    this.data.fileList1.concat(file)
    console.log('fileList',this.data.fileList1)
  },
  oversize() {
    wx.showToast({ title: '文件超出大小限制', icon: 'none' });
  },
  onCloseRoom(){
    this.setData({
      isShowPopupRoom: !this.data.isShowPopupRoom
    })
  },
  goBandRoom(){
    wx.navigateTo({
      url: '/pagesProperty/pages/feeOrder/index',
    })
  }
})